/**
 * All Rights Reserved.
 * This software is the confidential and proprietary information of
 * Beijing Audiotone Technology Co.,Ltd.
 * ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with Audiotone.
 * AUDIOTONE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUDIOTONE SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package pro.uccs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import pro.uccs.constant.ISVErrorOauthCode;
import pro.uccs.constant.OpenPlatformConstant;
import pro.uccs.service.Oauth2Service;
import pro.uccs.util.Coder;
import pro.uccs.util.EncryptUtil;
import pro.uccs.util.MethodUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * @author songc
 */
@Controller
@RequestMapping("/open")
public class DemoController {

    private Oauth2Service openOauth2Service;

    /**
     * 申请添加好友
     */
    @RequestMapping(value = "/applyFriend", method = RequestMethod.GET,produces="text/html;charset=UTF-8")
    @ResponseBody
    public Object applyFriend(String userNumber,String friendNumber) throws IOException {
        Map<String, String> params = MethodUtil.getFriendApplyParam(userNumber,friendNumber);
        MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            postParameters.add(entry.getKey(),entry.getValue());
        }
        String body = openOauth2Service.getResourceByAuthorization(postParameters);
        return body;
    }

    /**
     * 批量获取指定用户公开信息
     *
     */
    @RequestMapping(value = "/getUserPublicBatch", method = RequestMethod.GET,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getUserPublic(String userNumber ) throws IOException {
        Map<String, String> params = MethodUtil.getPublicParam(userNumber);
        MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            postParameters.add(entry.getKey(),entry.getValue());
        }
        String body = openOauth2Service.getResourceByAuthorization(postParameters);
        return body;
    }

    /**
     * 查询指定用户是否好友
     *
     * @return
     */
    @RequestMapping(value = "/isFriend", method = RequestMethod.GET,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String isFriend(String userNumber) throws IOException {
        Map<String, String> params = MethodUtil.getIsFriendParam(userNumber);
        MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            postParameters.add(entry.getKey(),entry.getValue());
        }
        String body = openOauth2Service.getResourceByAuthorization(postParameters);
        return body;
    }

    public void setOpenOauth2Service(Oauth2Service openOauth2Service) {
        this.openOauth2Service = openOauth2Service;
    }

}
